<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Entity\NhanVien;
use Symfony\Component\HttpFoundation\Response;

class NhanVienController extends Controller
{
	public function getnhanvienAction()
	{
		
		$request = $this->get('request');
		
		$hoten = $request->request->get('hoten') ? strval($request->request->get('hoten')) : "";
		$phongbanid = $request->request->get('phongban') ? strval($request->request->get('phongban')) : "";
		$trinhdoid = $request->request->get('trinhdo') ? strval($request->request->get('trinhdo')) : "";
		$chucvuid = $request->request->get('chucvu') ? strval($request->request->get('chucvu')) : "";
		$lydoid = $request->request->get('lydo') ? strval($request->request->get('lydo')) : "1";
	
		$page = $request->request->get('page') ? intval($request->request->get('page')) : 1;
		$rows = $request->request->get('rows') ? intval($request->request->get('rows')) : 10;
		

		$offset = ($page-1)*$rows;
		
		
		$filter = array("hoten"=>$hoten,"phongbanid"=>$phongbanid,"trinhdoid"=>$trinhdoid,"chucvuid"=>$chucvuid,"lydoid"=>$lydoid
		);
		
		$listNhanVien =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->findByCoditions($offset,$rows,$filter);

		$countNhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')->countAll($filter);
		
		$result = array();
		$result["total"] = \Count($countNhanVien);
		
		$items = array();
		
		
		foreach ($listNhanVien as $nhanvien){
			
			if($nhanvien->getTinhbaohiem() == "1"){
				$tinhbaohiem = "Có";
			} else {
				$tinhbaohiem = "Không";
			}
			
			if($nhanvien->getTinhantrua() == "1"){
				$tinhantrua = "Có"; 
			} else {
				$tinhantrua = "Không";
			}
			
			$ngayvaodang="";
			$ngaysinh = $nhanvien->getNgaysinh()->format('d-m-Y');
			$ngayvaolam = $nhanvien->getNgayvaolam()->format('d-m-Y');
			if($nhanvien->getNgayvaodang()){
			$ngayvaodang = $nhanvien->getNgayvaodang()->format('d-m-Y');
			}
			$item = array("id1"=>$nhanvien->getId(),"id2"=>$nhanvien->getId(),"hoten"=>$nhanvien->getHoten(),
					"diachi"=>$nhanvien->getDiachi(),
					"ngaysinh"=>$ngaysinh,
					"dienthoai"=>$nhanvien->getDienthoai(),
					"phongban"=>$nhanvien->getPhongbanid()->getTen(),
					"chucvu"=>$nhanvien->getChucvuid()->getTen(),
					"ngayvaolam"=>$ngayvaolam,
					"ngayvaodang"=>$ngayvaodang,
					"trinhdo"=>$nhanvien->getTrinhdoid()->getTen(),
					"mucluong"=>$nhanvien->getMucluong(),
					"hesoluongcv"=>$nhanvien->getHesoluongcv(),
					"tinhbaohiem"=>$tinhbaohiem,
					"tinhantrua"=>$tinhantrua,
					"tienphucap"=>$nhanvien->getTienphucap(),
					"lg"=>$nhanvien->getLg(),
					"giamtrugiacanh"=>$nhanvien->getGiamtrugiacanh()
					
			);
			
			array_push($items, $item);
		}
		
		$result["rows"] = $items;
		
			
		$response = new Response(json_encode($result));
		$response->headers->set('Content-Type', 'application/json');
	
		return $response;
	}
	
	public function getphongbanAction()
	{
		$lstphongban = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:PhongBan')
		->findAllByCoditions();
		$count = 0;
		$result = "";
		$result .= '{"total":'.\Count($lstphongban).',"rows":[';
		foreach ($lstphongban as $phongban) {
			$count++;
			if($count>= \Count($lstphongban)) {
				$result .= '{"id":"'. $phongban->getMa() .'","name":"'. $phongban->getTen() .'","nhom":"'. $phongban->getNhomid()->getTen() .'"}';
			}else {
				$result .= '{"id":"'. $phongban->getMa() .'","name":"'. $phongban->getTen() .'","nhom":"'. $phongban->getNhomid()->getTen() .'"},';
			}
		}
	
		$result .= ']}';
	
		$response = new Response($result);
		$response->headers->set('Content-Type', 'application/json');
	
		return $response;
	}
	
	public function getchucvuAction()
	{
		$lstChucVu = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:ChucVu')
		->findAllByCoditions();
		$count = 0;
		$result = "";
		$result .= '{"total":'.\Count($lstChucVu).',"rows":[';
		foreach ($lstChucVu as $ChucVu) {
			$count++;
			if($count>= \Count($lstChucVu)) {
				$result .= '{"id":"'. $ChucVu->getMa() .'","name":"'. $ChucVu->getTen() .'","heso":"'. $ChucVu->getHeso() .'"}';
			}else {
				$result .= '{"id":"'. $ChucVu->getMa() .'","name":"'. $ChucVu->getTen() .'","heso":"'. $ChucVu->getHeso() .'"},';
			}
		}
	
		$result .= ']}';
	
		$response = new Response($result);
		$response->headers->set('Content-Type', 'application/json');
	
		return $response;
	}
	public function gettrinhdoAction()
	{
		$lstTrinhDo = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->findAllByCoditions();
		$count = 0;
		$result = "";
		$result .= '{"total":'.\Count($lstTrinhDo).',"rows":[';
		foreach ($lstTrinhDo as $TrinhDo) {
			$count++;
			if($count>= \Count($lstTrinhDo)) {
				$result .= '{"id":"'. $TrinhDo->getMa() .'","name":"'. $TrinhDo->getTen() .'"}';
			}else {
				$result .= '{"id":"'. $TrinhDo->getMa() .'","name":"'. $TrinhDo->getTen() .'"},';
			}
		}
	
		$result .= ']}';
	
		$response = new Response($result);
		$response->headers->set('Content-Type', 'application/json');
	
		return $response;
	}
	
	private function getlist ($thongbao,$filter) {

		$lsttrinhdo = $this->getListTrinhDo();
		$lstphongban = $this->getListPhongBan();
		$lstchucvu = $this->getListChucVu();

	

		return $this->render(
				'AcmeDemoBundle:Demo:NhanVien_list.html.twig',
				array(	
						'lsttrinhdo' => $lsttrinhdo,
						'lstphongban' => $lstphongban,
						'lstchucvu' => $lstchucvu,
						'thongbao' => $thongbao,
						'filter'=>$filter
				)
		);
	}
	public function getListLyDo(){
		return $this->getDoctrine()
		->getRepository('AcmeDemoBundle:LyDoNghiViec')->findAllByCoditions();
	}
	public function getListTrinhDo(){
		return $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')->findAllByCoditions();
	}
	public function getListPhongBan(){
		return $this->getDoctrine()
		->getRepository('AcmeDemoBundle:PhongBan')->findAllByCoditions();
	}
	public function getListChucVu(){
		return $this->getDoctrine()
		->getRepository('AcmeDemoBundle:ChucVu')->findAllByCoditions();
	}
	public function inputAction()
	{
		
		$request = $this->get('request');
		if ($request->isMethod('POST')) {

			$hoten = $request->request->get('hoten');
			$diachi = $request->request->get('diachi');
			$ngaysinh = $request->request->get('ngaysinh');
			$arr = explode('-', $ngaysinh);
			$ngaysinh = $arr[2].'-'.$arr[1].'-'.$arr[0];


			$dienthoai = $request->request->get('dienthoai');
			$phongbanma = $request->request->get('phongban');
			$phongban = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:PhongBan')
			->findOneByMa($phongbanma);

			$chucvuma = $request->request->get('chucvu');
			$chucvu = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:ChucVu')
			->findOneByMa($chucvuma);

			$ngayvaolam = $request->request->get('ngayvaolam');
			$arr = explode('-', $ngayvaolam);
			$ngayvaolam = $arr[2].'-'.$arr[1].'-'.$arr[0];

			$ngayvaodang = $request->request->get('ngayvaodang');
			if($ngayvaodang != "") {
				$arr = explode('-', $ngayvaodang);
				$ngayvaodang = $arr[2].'-'.$arr[1].'-'.$arr[0];
			}

			$trinhdoma = $request->request->get('trinhdo');
			$trinhdo = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:TrinhDo')
			->findOneByMa($trinhdoma);

			$mucluong = $request->request->get('mucluong');
			$hesocongviec = $request->request->get('hesocongviec');
			$tinhbaohiem = $request->request->get('tinhbaohiem');
			$tinhantrua = $request->request->get('tinhantrua');

			$tienphucap = str_replace(',','',$request->request->get('tienphucap'));
			$lg = $request->request->get('lg');
			$giamtrugiacanh = $request->request->get('giamtrugiacanh');
			if(!$phongban){
				return $this->render(
						'AcmeDemoBundle:Demo:NhanVien_input.html.twig',
						array(
								
								'exist'=>"phongban",
								'hoten'=>$hoten,
								'diachi'=>$diachi,
								'dienthoai'=>$dienthoai,
								'ngaysinh'=>$ngaysinh,
								'maphongban'=>$phongbanma,
								'machucvu'=>$chucvuma,
								'ngayvaolam'=>$ngayvaolam,
								'ngayvaodang'=>$ngayvaodang,
								'matrinhdo'=>$trinhdoma,
								'mucluong'=>$mucluong,
								'hesocongviec'=>$hesocongviec,
								'tinhbaohiem'=>$tinhbaohiem,
								'tinhantrua'=>$tinhantrua,
								'tienphucap'=>$tienphucap,
								'lg'=>$lg,
								'giamtrugiacanh'=>$giamtrugiacanh
									
						)
				);
			}
			if(!$chucvu){
				return $this->render(
						'AcmeDemoBundle:Demo:NhanVien_input.html.twig',
						array(
								
								
								'exist'=>"chucvu",
								'hoten'=>$hoten,
								'diachi'=>$diachi,
								'dienthoai'=>$dienthoai,
								'ngaysinh'=>$ngaysinh,
								'maphongban'=>$phongbanma,
								'machucvu'=>$chucvuma,
								'ngayvaolam'=>$ngayvaolam,
								'ngayvaodang'=>$ngayvaodang,
								'matrinhdo'=>$trinhdoma,
								'mucluong'=>$mucluong,
								'hesocongviec'=>$hesocongviec,
								'tinhbaohiem'=>$tinhbaohiem,
								'tinhantrua'=>$tinhantrua,
								'tienphucap'=>$tienphucap,
								'lg'=>$lg,
								'giamtrugiacanh'=>$giamtrugiacanh
									
						)
				);
			}
			if(!$trinhdo){
				return $this->render(
						'AcmeDemoBundle:Demo:NhanVien_input.html.twig',
						array(
								
								
								'exist'=>"trinhdo",
								'hoten'=>$hoten,
								'diachi'=>$diachi,
								'ngaysinh'=>$ngaysinh,
								'dienthoai'=>$dienthoai,
								'maphongban'=>$phongbanma,
								'machucvu'=>$chucvuma,
								'ngayvaolam'=>$ngayvaolam,
								'ngayvaodang'=>$ngayvaodang,
								'matrinhdo'=>$trinhdoma,
								'mucluong'=>$mucluong,
								'hesocongviec'=>$hesocongviec,
								'tinhbaohiem'=>$tinhbaohiem,
								'tinhantrua'=>$tinhantrua,
								'tienphucap'=>$tienphucap,
								'lg'=>$lg,
								'giamtrugiacanh'=>$giamtrugiacanh
									
						)
				);
			}



			//set vao


			$NhanVien = new NhanVien();

			$NhanVien->setHoten($hoten);
			$NhanVien->setDiachi($diachi);
			$NhanVien->setNgaysinh(new \DateTime($ngaysinh . " 00:00:00"));
			$NhanVien->setDienthoai($dienthoai);
			$NhanVien->setPhongbanid($phongban);
			$NhanVien->setChucvuid($chucvu);
			$NhanVien->setNgayvaolam(new \DateTime($ngayvaolam . " 00:00:00"));
			if($ngayvaodang != "") {
				$NhanVien->setNgayvaodang(new \DateTime($ngayvaodang . " 00:00:00"));
			} else {
				$NhanVien->setNgayvaodang(null);
			}
			$NhanVien->setTrinhdoid($trinhdo);
			$NhanVien->setMucluong($mucluong);
			$NhanVien->setHesoluongcv($hesocongviec);
				
			$NhanVien->setThutu($chucvu->getThutu());
			
			if($tinhbaohiem == "on"){
				$NhanVien->setTinhbaohiem(1);
			} else {
				$NhanVien->setTinhbaohiem(0);
			}
			if($tinhantrua == "on"){
				$NhanVien->setTinhantrua(1);
			} else {
				$NhanVien->setTinhantrua(0);
			}

			$NhanVien->setTienphucap($tienphucap);
			$NhanVien->setLg($lg);
			$NhanVien->setGiamtrugiacanh($giamtrugiacanh);
			$NhanVien->setCreated(new \DateTime('now'));
			$this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->saveNhanVien($NhanVien);

			$filter = array("hoten"=>"","phongbanid"=>"","trinhdoid"=>"","chucvuid"=>"","lydoid"=>"1"
			);
			return $this->getlist("ok",$filter);

		}

		return $this->render(
				'AcmeDemoBundle:Demo:NhanVien_input.html.twig',
				array(
						
						'exist'=>null,
						'hoten'=>"",
						'diachi'=>"",
						'dienthoai'=>"",
						'ngaysinh'=>"",
						'maphongban'=>"",
						'machucvu'=>"",
						'ngayvaolam'=>"",
						'ngayvaodang'=>"",
						'matrinhdo'=>"",
						'mucluong'=>"",
						'hesocongviec'=>"",
						'tinhbaohiem'=>"",
						'tinhantrua'=>"",
						'tienphucap'=>"",
						'lg'=>"",
						'giamtrugiacanh'=>""
							
				)
		);
	}
	public function listAction()
	{
		$filter = array("hoten"=>"","phongbanid"=>"","trinhdoid"=>"","chucvuid"=>"","lydoid"=>"1"
		);
		return $this->getlist(null,$filter);

	}
	public function detailAction($id)
	{
		$request = $this->get('request');

		$hoten = $request->request->get('hoten') ? strval($request->request->get('hoten')) : "";
		$phongbanid = $request->request->get('phongban') ? strval($request->request->get('phongban')) : "";
		$trinhdoid = $request->request->get('trinhdo') ? strval($request->request->get('trinhdo')) : "";
		$chucvuid = $request->request->get('chucvu') ? strval($request->request->get('chucvu')) : "";
		$lydoid = $request->request->get('lydo') ? strval($request->request->get('lydo')) : "1";
		$filter = array("hoten"=>$hoten,"phongbanid"=>$phongbanid,"trinhdoid"=>$trinhdoid,"chucvuid"=>$chucvuid,"lydoid"=>$lydoid
		);
		
		
		
		$NhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->find($id);
			
		return $this->render(
				'AcmeDemoBundle:Demo:NhanVien_edit.html.twig',
				array(
						
						'exist'=>null,
						'nhanvien'=>$NhanVien,
						'filter'=>$filter
							
				)
		);
	}
	public function editAction($id)
	{
		
		$NhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->find($id);

		$request = $this->get('request');
		
		$hotenhi = $request->request->get('hi_hoten') ? strval($request->request->get('hi_hoten')) : "";
		$phongbanid = $request->request->get('hi_phongban') ? strval($request->request->get('hi_phongban')) : "";
		$trinhdoid = $request->request->get('hi_trinhdo') ? strval($request->request->get('hi_trinhdo')) : "";
		$chucvuid = $request->request->get('hi_chucvu') ? strval($request->request->get('hi_chucvu')) : "";
		$lydoid = $request->request->get('hi_lydo') ? strval($request->request->get('hi_lydo')) : "1";
		$filter = array("hoten"=>$hotenhi,"phongbanid"=>$phongbanid,"trinhdoid"=>$trinhdoid,"chucvuid"=>$chucvuid,"lydoid"=>$lydoid
		);
		
		
		
		
		$hoten = $request->request->get('hoten');
		$diachi = $request->request->get('diachi');
		$ngaysinh = $request->request->get('ngaysinh');
		$arr = explode('-', $ngaysinh);
		$ngaysinh = $arr[2].'-'.$arr[1].'-'.$arr[0];
			
			
		$dienthoai = $request->request->get('dienthoai');
		$phongbanma = $request->request->get('phongban');
		$phongban = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:PhongBan')
		->findOneByMa($phongbanma);
			
		$chucvuma = $request->request->get('chucvu');
		$chucvu = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:ChucVu')
		->findOneByMa($chucvuma);
			
		$ngayvaolam = $request->request->get('ngayvaolam');
		$arr = explode('-', $ngayvaolam);
		$ngayvaolam = $arr[2].'-'.$arr[1].'-'.$arr[0];
			
		$ngayvaodang = $request->request->get('ngayvaodang');
		if($ngayvaodang != "") {
			$arr = explode('-', $ngayvaodang);
			$ngayvaodang = $arr[2].'-'.$arr[1].'-'.$arr[0];
		}
		
			
		$trinhdoma = $request->request->get('trinhdo');
		$trinhdo = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->findOneByMa($trinhdoma);
			
		$mucluong = $request->request->get('mucluong');
		$hesocongviec = $request->request->get('hesocongviec');
		$tinhbaohiem = $request->request->get('tinhbaohiem');
		$tinhantrua = $request->request->get('tinhantrua');
			
		$tienphucap = str_replace(',','',$request->request->get('tienphucap'));
		$lg = $request->request->get('lg');
		$giamtrugiacanh = $request->request->get('giamtrugiacanh');


		if(!$phongban){
			return $this->render(
					'AcmeDemoBundle:Demo:NhanVien_edit.html.twig',
					array(
							
							'exist'=>"phongban",
							'nhanvien'=>$NhanVien,
						'filter'=>$filter
								
					)
			);
		}
		if(!$chucvu){
			return $this->render(
					'AcmeDemoBundle:Demo:NhanVien_edit.html.twig',
					array(
							
							'exist'=>"chucvu",
							'nhanvien'=>$NhanVien,
						'filter'=>$filter
								
					)
			);
		}
		if(!$trinhdo){
			return $this->render(
					'AcmeDemoBundle:Demo:NhanVien_edit.html.twig',
					array(
							
							'exist'=>"trinhdo",
							'nhanvien'=>$NhanVien,
						'filter'=>$filter
								
					)
			);
		}

		$NhanVien->setHoten($hoten);
		$NhanVien->setDiachi($diachi);
		$NhanVien->setNgaysinh(new \DateTime($ngaysinh . " 00:00:00"));
		$NhanVien->setDienthoai($dienthoai);
		$NhanVien->setPhongbanid($phongban);
		$NhanVien->setChucvuid($chucvu);
		$NhanVien->setNgayvaolam(new \DateTime($ngayvaolam . " 00:00:00"));
		if($ngayvaodang != "") {
			$NhanVien->setNgayvaodang(new \DateTime($ngayvaodang . " 00:00:00"));
		} else {
			$NhanVien->setNgayvaodang(null);
		}
		
		$NhanVien->setTrinhdoid($trinhdo);
		$NhanVien->setMucluong($mucluong);
		$NhanVien->setHesoluongcv($hesocongviec);

		if($tinhbaohiem == "on"){
			$NhanVien->setTinhbaohiem(1);
		} else {
			$NhanVien->setTinhbaohiem(0);
		}
		if($tinhantrua == "on"){
			$NhanVien->setTinhantrua(1);
		} else {
			$NhanVien->setTinhantrua(0);
		}

		$NhanVien->setTienphucap($tienphucap);
		$NhanVien->setLg($lg);
		$NhanVien->setGiamtrugiacanh($giamtrugiacanh);
		$NhanVien->setUpdated(new \DateTime('now'));
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->saveNhanVien($NhanVien);
		
		return $this->getlist("ok",$filter);


	}
	public function delAction($id)
	{

		$request = $this->get('request');
		
		
		$hotenhi = $request->request->get('hi_hoten') ? strval($request->request->get('hi_hoten')) : "";
		$phongbanid = $request->request->get('hi_phongban') ? strval($request->request->get('hi_phongban')) : "";
		$trinhdoid = $request->request->get('hi_trinhdo') ? strval($request->request->get('hi_trinhdo')) : "";
		$chucvuid = $request->request->get('hi_chucvu') ? strval($request->request->get('hi_chucvu')) : "";
		$lydoid = $request->request->get('hi_lydo') ? strval($request->request->get('hi_lydo')) : "1";
		$filter = array("hoten"=>$hotenhi,"phongbanid"=>$phongbanid,"trinhdoid"=>$trinhdoid,"chucvuid"=>$chucvuid,"lydoid"=>$lydoid
		);
		
		
		$lstlydo = $this->getListLyDo();
		$NhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->find($id);
		
		return $this->render(
				'AcmeDemoBundle:Demo:NhanVien_del.html.twig',
				array('lstlydo' => $lstlydo,
						'id' => $id,'hoten'=>$NhanVien->getHoten(),
						'filter'=>$filter
				)
		);
	}
	public function delokAction($id)
	{
	
		$request = $this->get('request');
		$hotenhi = $request->request->get('hi_hoten') ? strval($request->request->get('hi_hoten')) : "";
		$phongbanid = $request->request->get('hi_phongban') ? strval($request->request->get('hi_phongban')) : "";
		$trinhdoid = $request->request->get('hi_trinhdo') ? strval($request->request->get('hi_trinhdo')) : "";
		$chucvuid = $request->request->get('hi_chucvu') ? strval($request->request->get('hi_chucvu')) : "";
		$lydoid = $request->request->get('hi_lydo') ? strval($request->request->get('hi_lydo')) : "1";
		$filter = array("hoten"=>$hotenhi,"phongbanid"=>$phongbanid,"trinhdoid"=>$trinhdoid,"chucvuid"=>$chucvuid,"lydoid"=>$lydoid
		);
		
		
		
		$NhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->find($id);
		
	
		$lydoid = $request->request->get('lydo');

			
		$lydo = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')
		->findOneById($lydoid);

		$NhanVien->setDeleted(new \DateTime('now'));
		$NhanVien->setLydoid($lydo);

		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->saveNhanVien($NhanVien);
		
		return $this->getlist("ok",$filter);
		
	}

}
